package com.situ.crm.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.crm.bean.pojo.User;
import com.situ.crm.bean.vo.RouterVO;
import com.situ.crm.rbac.mapper.UserMapper;
import com.situ.crm.rbac.service.IPermissionService;
import com.situ.crm.rbac.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2022-11-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IPermissionService permissionService;

    @Override
    public void updateStatus(Long id, Integer status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        baseMapper.updateById(user);
    }

    @Override
    public User selectByName(String username) {
        return baseMapper.selectOne(new QueryWrapper<User>().eq("name", username));
    }

    @Override
    public Map<String, Object> getUserPermissionInfo(String username) {
        //根据username查询用户的基本信息
        User user = selectByName(username);
        //根据user id查询所有的路由权限（目录权限+菜单权限）
        List<RouterVO> routerVOList = permissionService.selectRouterListByUser(user);
        //根据user id查询所有的按钮权限
        List<String> btnList = permissionService.selectBtnListByUser(user);

        Map<String, Object> map = new HashMap<>();
        map.put("name", user.getName());
        map.put("avatar", user.getAvatar());
        //路由权限
        map.put("routers", routerVOList);
        //按钮权限
        map.put("btns", btnList);
        return map;
    }
}
